package com.tssf.car_manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tssf.car_manage.entity.Vehicle;
import com.tssf.car_manage.mapper.CustomerMapper;
import com.tssf.car_manage.mapper.VehicleMapper;
import com.tssf.car_manage.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    VehicleMapper vehicleMapper;
    @Autowired
    CustomerMapper customerMapper;

    @Override
    public List<Vehicle> lists() {
        return vehicleMapper.lists();
    }

    @Override
    public PageInfo<Vehicle> listWithPage(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Vehicle> list = vehicleMapper.lists();
        return new PageInfo<>(list);
    }

    @Override
    public List<Vehicle> Sealist(String license_plate) {
        if (license_plate == null || license_plate.trim().isEmpty()) {
            return vehicleMapper.lists();
        } else {
            return vehicleMapper.Sealist("%"+license_plate+"%");
        }
    }

    @Override
    public PageInfo<Vehicle> searchWithPage(String key, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Vehicle> list;
        if (key == null || key.trim().isEmpty()) {
            list = vehicleMapper.lists();
        } else {
            list = vehicleMapper.Sealist("%"+key+"%");
        }
        return new PageInfo<>(list);
    }

    @Override
    public void delVehicle(int vehicle_id) {
        vehicleMapper.delVehicle(vehicle_id);
    }

    @Override
    public int addvehicle(Vehicle vehicle) {
        return vehicleMapper.addvehicle(vehicle);
    }

    @Override
    public Integer getNextVehicleId(){
        Integer maxId = vehicleMapper.getMaxVehicleId();
        return maxId != null ? maxId + 1 : 1;
    }

    @Override
    public int update(Vehicle vehicle) {
        return vehicleMapper.update(vehicle);
    }

    @Override
    public Vehicle getById(int vehicle_id) {
        return vehicleMapper.getById(vehicle_id);
    }
}